﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using System.Data;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Globalization;
using System.Numerics;
using System.Reflection;

namespace IHISCETeim.Web
{
    /// <summary>
    /// 页面操作类型
    /// </summary>
    public enum PageAction
    { 
        /// <summary>
        /// 查找
        /// </summary>
        query,
        query2,
        query3,
        /// <summary>
        /// 删除单行
        /// </summary>
        delrow,
        /// <summary>
        /// 删除多行
        /// </summary>
        delrows,
        /// <summary>
        /// 添加
        /// </summary>
        add,
        /// <summary>
        /// 编辑单行
        /// </summary>
        editrow,
        /// <summary>
        /// 根据ID获取信息
        /// </summary>
        getbyid
    }

    public class EasyUIPageBase:PageBase
    {


        protected void Page_Load(object sender, EventArgs e)
        {
          
           
            string ajaxmethod = GetRequestValue<string>("action", "");
            if (ajaxmethod != "")
            {
                Response.Buffer = true;
                Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
                Response.AddHeader("pragma", "no-cache");
                Response.AddHeader("cache-control", "");
                Response.CacheControl = "no-cache";
                Response.ContentType = "application/json";
                

                PageAction action = (PageAction)Enum.Parse(typeof(PageAction), ajaxmethod.Trim().ToLower());
                string temp = "";
                switch (action)
                {
                    case PageAction.query:
                        temp = query();
                        break;
                    case PageAction.query2:
                        temp = query2();
                        break;
                    case PageAction.query3:
                        temp = query3();
                        break;
                    case PageAction.delrow:
                        temp = delrow();
                        break;
                    case PageAction.delrows:
                        temp = delrows();
                        break;
                    case PageAction.add:
                        temp = add();
                        break;
                    case PageAction.editrow:
                        temp = editrow();
                        break;                    
                }

                Response.Write(temp);
                Response.End();
            }
        }
        /// <summary>
        /// 自定义查询处理
        /// </summary>
        /// <returns></returns>
        protected virtual string query() {
            return "自定义查询处理";
        }
        protected virtual string query2()
        {
            return "自定义查询处理2";
        }
        protected virtual string query3()
        {
            return "自定义查询处理3";
        }
        /// <summary>
        /// 自定义删除单条处理
        /// </summary>
        /// <returns></returns>
        protected virtual string delrow()
        {
            return "自定义删除单条处理";
        }
        /// <summary>
        /// 自定义删除多条处理
        /// </summary>
        /// <returns></returns>
        protected virtual string delrows()
        {
            return "自定义删除多条处理";
        }
        /// <summary>
        /// 自定义添加处理
        /// </summary>
        /// <returns></returns>
        protected virtual string add()
        {
            return "自定义添加处理";
        }
        /// <summary>
        /// 自定义编辑行处理
        /// </summary>
        /// <returns></returns>
        protected virtual string editrow()
        {
            return "自定义编辑行处理";
        }

       
        protected string HasPowerEasyUI(Permission per)
        {
            return HasPower(per).ToString().ToLower();
        }


        #region easyui
        /// <summary>
        /// 当前显示页
        /// </summary>
        protected override int pageNum
        {
            get
            {
                return GetRequestValue<int>("page", 1);
            }
        }
        /// <summary>
        /// 每页显示数量
        /// </summary>
        protected override int numPerPage
        {
            get
            {
                return GetRequestValue<int>("rows", GetMySettingValue("MyNumPerPage") == "" ? 20 : Convert.ToInt32(GetMySettingValue("MyNumPerPage")));
                // return GetRequestValue<int>("rows", 20);
            }
        }

        /// <summary>
        /// 排序字段 值为 id
        /// </summary>
        protected override string orderField
        {
            get
            {
                return GetRequestValue<string>("sort","");
            }
        }
        /// <summary>
        ///  排序方式 desc或 asc
        /// </summary>
        protected override string orderDirection
        {
            get
            {
                return GetRequestValue<string>("order", "ASC");
            }
        }
        /// <summary>
        /// 当设置分页属性时，初始化每页记录数列表。
        /// </summary>
        /// <returns></returns>
        protected new string SetnumPerPage()
        {
            return "[10,20,50,100,150,200]";
        }

        #endregion
             
        /// <summary>
        /// 转换成datagrid格式的数据 json
        /// </summary>
        /// <param name="dt">要显示的数据集合</param>
        /// <param name="total">总数</param>
        /// <param name="sumdt">汇总行的数据,只取第一行，列名必需和dt中的列名一致</param>
        /// <returns></returns>
        public StringBuilder ToDataGridJson(DataTable dt, int total, DataTable sumdt)
        {
            JsonSerializerHelper jsonHelper = new JsonSerializerHelper();

            StringBuilder jsonData = new StringBuilder();

            jsonData.Append("{");
            jsonData.Append("\"total\":");
            jsonData.Append(total);
            jsonData.Append(",");
            jsonData.Append("\"rows\":");
            jsonData.Append("[");

            foreach (DataRow item in dt.Rows)
            {
                string jsonGroup = jsonHelper.Serialize(item);
                jsonData.Append(jsonGroup);
                jsonData.Append(",");
            }

            if (dt.Rows.Count > 0)
                jsonData = jsonData.Remove(jsonData.Length - 1, 1);

            jsonData.Append("]");
            if ( sumdt !=null)
            {
                //显示页脚
                jsonData.Append(",");
                jsonData.Append("\"footer\":");
                jsonData.Append("[");

                foreach (DataRow item in sumdt.Rows)
                {
                    string jsonGroup = jsonHelper.Serialize(item);
                    jsonData.Append(jsonGroup);
                    jsonData.Append(",");
                }

                if (sumdt.Rows.Count > 0)
                    jsonData = jsonData.Remove(jsonData.Length - 1, 1);
                jsonData.Append("]");
            }
            //
            jsonData.Append("}");
            return jsonData;
        }
    }
}